<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 29] 29.6 java.text.Collator (JDK 1.1)</TITLE>
<META NAME="author" CONTENT="David Flanagan">
<META NAME="date" CONTENT="Thu Jul 31 16:32:59 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java">
<META NAME="title" CONTENT="Java in a Nutshell">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java in a Nutshell" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch29_05.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 29<br>The java.text Package</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch29_07.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JNUT2-CH-29-SECT-6">29.6 java.text.Collator (JDK 1.1)</A></h2>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JNUT2-CH-29-SECT-6.1"></A></h3>
</DIV>

<P CLASS=para>
This class is used to compare, order, and sort strings in a
way that is appropriate for the default locale or some other
specified locale.  Because it is an abstract class, it
cannot be instantiated directly.  Instead, you must use the
static <tt CLASS=literal>getInstance()</tt> method to obtain an instance of
a <tt CLASS=literal>Collator</tt> subclass that is appropriate for the
default or specified locale.  You can use
<tt CLASS=literal>getAvailableLocales()</tt> to determine whether a
<tt CLASS=literal>Collator</tt> object is available for a desired locale.

<P CLASS=para>
Once an appropriate <tt CLASS=literal>Collator</tt> object has been
obtained, you can use the <tt CLASS=literal>compare()</tt> method to
compare strings.  The possible return values of this method
are -1, 0, and 1, which indicate, respectively, that the
first string is collated before the second, that the two are
equivalent for collation purposes, and that the first string
is collated after the second.  The <tt CLASS=literal>equals()</tt> method
is a convenient shortcut for testing two strings for
collation equivalence.

<P CLASS=para>
When sorting an array of strings, each string in the array
is typically compared more than once.  Using the
<tt CLASS=literal>compare()</tt> method in this case is inefficient. A
more efficient method for comparing strings multiple times is to use
<tt CLASS=literal>getCollationKey()</tt> for each string to create
<tt CLASS=literal>CollationKey</tt> objects.  These objects can then be
compared to each other more quickly than the strings
themselves could be compared.

<P CLASS=para>
You can customize the way the <tt CLASS=literal>Collator</tt> object
performs comparisons by calling <tt CLASS=literal>setStrength()</tt>.  If
you pass the constant <tt CLASS=literal>PRIMARY</tt> to this method,
the comparison only looks at primary differences in the
strings--it compares letters but ignores accents and case
differences.  If you pass the constant <tt CLASS=literal>SECONDARY</tt>,
it ignores case differences but does not ignore
accents.  And if you pass <tt CLASS=literal>TERTIARY</tt> (the default), the
<tt CLASS=literal>Collator</tt> object takes both accents and case
differences into account in its comparison.

<DIV CLASS=screen>
<P>
<PRE>
public abstract class <tt CLASS=literal>Collator</tt> extends Object implements Cloneable, Serializable {
    // <tt CLASS=literal>Protected Constructor</tt>
            protected <tt CLASS=literal>Collator</tt>();
    // <tt CLASS=literal>Constants</tt>
            public static final int <tt CLASS=literal>CANONICAL_DECOMPOSITION</tt>;
            public static final int <tt CLASS=literal>FULL_DECOMPOSITION</tt>;
            public static final int <tt CLASS=literal>IDENTICAL</tt>;
            public static final int <tt CLASS=literal>NO_DECOMPOSITION</tt>;
            public static final int <tt CLASS=literal>PRIMARY</tt>;
            public static final int <tt CLASS=literal>SECONDARY</tt>;
            public static final int <tt CLASS=literal>TERTIARY</tt>;
    // <tt CLASS=literal>Class Methods</tt>
            public static synchronized Locale[] <tt CLASS=literal>getAvailableLocales</tt>();
            public static synchronized Collator <tt CLASS=literal>getInstance</tt>();
            public static synchronized Collator <tt CLASS=literal>getInstance</tt>(Locale <tt CLASS=literal>desiredLocale</tt>);
    // <tt CLASS=literal>Public Instance Methods</tt>
            public Object <tt CLASS=literal>clone</tt>();  //<tt CLASS=literal> Overrides Object</tt>
            public abstract int <tt CLASS=literal>compare</tt>(String <tt CLASS=literal>source</tt>, String <tt CLASS=literal>target</tt>);
            public boolean <tt CLASS=literal>equals</tt>(String <tt CLASS=literal>source</tt>, String <tt CLASS=literal>target</tt>);
            public boolean <tt CLASS=literal>equals</tt>(Object <tt CLASS=literal>that</tt>);  //<tt CLASS=literal> Overrides Object</tt>
            public abstract CollationKey <tt CLASS=literal>getCollationKey</tt>(String <tt CLASS=literal>source</tt>);
            public synchronized int <tt CLASS=literal>getDecomposition</tt>();
            public synchronized int <tt CLASS=literal>getStrength</tt>();
            public abstract synchronized int <tt CLASS=literal>hashCode</tt>();  //<tt CLASS=literal> Overrides Object</tt>
            public synchronized void <tt CLASS=literal>setDecomposition</tt>(int <tt CLASS=literal>decompositionMode</tt>);
            public synchronized void <tt CLASS=literal>setStrength</tt>(int <tt CLASS=literal>newStrength</tt>);
}
</PRE>
</DIV>

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JNUT2-CH-29-SECT-6.2">Extended By:</A></h3>

<P CLASS=para>
RuleBasedCollator

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JNUT2-CH-29-SECT-6.3">Returned By:</A></h3>

<P CLASS=para>
Collator.getInstance()

</DIV>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch29_05.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch29_07.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>java.text.CollationKey (JDK 1.1)</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>java.text.DateFormat (JDK 1.1)</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
